<template>
  <div class="products-list" style="height: calc(100vh - 470px); overflow: auto">
    <div class="my-table" style="min-width: 1100px" v-if="list.length > 0">
      <div class="table-header">
        <div>
          <div class="row">
            <div class="col1" style="min-width: 160px">测量单编码</div>
            <div class="col6">商品种类信息</div>
            <div class="col5">数量</div>
            <div class="col5">件数</div>
            <div class="col4">采购商家</div>
            <div class="col4">测量前订单总金额</div>
            <div class="col4">测量后订单总金额</div>
            <div class="col5">税额</div>
            <div class="col5">是否开票</div>
            <div class="col4">下单时间</div>
            <div class="col4">测量时间</div>
            <div class="col6">操作</div>
          </div>
        </div>
      </div>

      <div class="table-body">
        <div class="list-item" v-for="(item, index) in list" :key="index">
          <div class="tpis-head">
            <div class="jzt-flex-start">
              <p class="orange" v-if="item.businessState === 1" style="width: 120px">待接单</p>
              <p class="orange" v-else-if="item.businessState === 2" style="width: 120px">待测量定价</p>
              <p class="green" v-else-if="item.businessState === 3" style="width: 120px">已测量并定价</p>
              <p class="red" v-else style="width: 120px">已撤销</p>

              <p>测量时间：{{ item.measureTime || "-" }}</p>
              <p class="jzt-flex-center ml32" v-if="[1, 2].includes(item.businessState)">
                <span style="color: #ff3333">测量倒计时：</span>
                <CountdownMy :timestamp="new Date(item.measureTime).getTime()" />
              </p>
            </div>
          </div>
          <div class="row">
            <div class="col1">
              <!-- 测量单编码 -->
              <el-button class="border" type="primary" link v-if="item.measureOrderNo" :title="item.measureOrderNo">
                {{ item.measureOrderNo }}
              </el-button>
              <span v-else>--</span>
            </div>
            <div class="col6">
              <!-- 商品种类信息 -->
              {{ item.categoryNum || 0 }}种商品/({{ item.detailNum || 0 }}件)
            </div>
            <div class="col5">
              <!-- 数量 -->
              {{ item.goodsNum }}
            </div>
            <div class="col5">
              <!-- 件数 -->
              {{ item.detailNum }}
            </div>
            <div class="col4">
              <!-- 采购商家 -->
              {{ item.merchantName }}
            </div>
            <div class="col4">
              <!-- 测量前订单总金额 -->
              {{ item.previousTotalAmount }}
            </div>
            <div class="col4">
              <!-- 测量后订单总金额 -->
              {{ item.goodsTotalAmount }}
            </div>
            <div class="col5">
              <!-- 税额 -->
              {{ item.invoiceAmount }}
            </div>
            <div class="col5">
              <!-- 是否开票 -->
              {{ item.isSupportBilling ? "是" : "否" }}
            </div>
            <div class="col4">
              <!-- 下单时间 -->
              {{ item.createTime }}
            </div>
            <div class="col4">
              <!-- 测量时间 -->
              {{ item.measureTime }}
            </div>
            <div class="col6">
              <!-- 操作 -->
              <div>
                <p>
                  <el-button
                    type="primary"
                    link
                    @click="toPath({ path: '/order/measure/detail', query: { measureOrderId: item.measureOrderId } })"
                  >
                    查看
                  </el-button>
                </p>
                <p v-if="item.businessState === 1">
                  <el-button type="primary" link @click="clickReceive(item)">确认接单</el-button>
                </p>
                <p v-if="item.businessState === 2">
                  <el-button
                    type="primary"
                    link
                    @click="toPath({ path: '/order/measure/detail', query: { measureOrderId: item.measureOrderId } })"
                    >录入数据并定价</el-button
                  >
                </p>
                <p v-if="item.businessState === 3">
                  <el-button
                    type="primary"
                    link
                    @click="toPath({ path: '/order/measure/detail', query: { measureOrderId: item.measureOrderId } })"
                    >修改录入数据</el-button
                  >
                </p>
                <p v-if="item.businessState === 4"><el-button type="danger" link @click="clickDel(item)">删除</el-button></p>
              </div>
            </div>
          </div>
          <div class="tips">
            <p class="mb4 f12">关联客户：{{ item.customer }}</p>
            <p class="f12">测量地址：{{ item.receivingName }} / {{ item.receivingMobile }} / {{ item.receivingAddress }}</p>
          </div>
        </div>
      </div>
    </div>
    <div v-else style="display: flex; justify-content: center; width: 100%">
      <el-empty description="这里暂时什么都没有" />
    </div>

    <!-- 确认接单 -->
    <ActionSubmitDialog
      v-model:isOpen="_isOpenReceive"
      :type="'info'"
      :title="'接单提醒'"
      :message="'接单后，请安排人员前往对应测量地点进行测量真实数据，并在当前订单中进行录入测量尺寸，以及测量后的商品定价，提交给采购商，请勿忘记哟~~'"
      :submitBtnText="'确认接单'"
      @submit="returnSubmitReceive"
    />

    <!-- 确认删除 -->
    <ActionSubmitDialog
      v-model:isOpen="_isOpenDel"
      :message="'您正在执行删除当前订单操作，删除后，当前订单将不在显示，是否继续删除？'"
      :submitBtnText="'确认删除'"
      @submit="returnSubmitDel"
    />
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
import CountdownMy from "./countdown";
import ActionSubmitDialog from "@/components/ActionSubmitDialog";
import { ElMessage } from "element-plus";
import { measureOrderDel, acceptOrder } from "@/apis/order/measure";

const emit = defineEmits(["refresh"]);
const props = defineProps({
  list: {
    type: Array,
    default: () => []
  }
});
const _isOpenReceive = ref(false);
const _isOpenDel = ref(false);
const currObj = ref({});

const clickReceive = row => {
  currObj.value = row;
  _isOpenReceive.value = true;
};
const clickDel = row => {
  currObj.value = row;
  _isOpenDel.value = true;
};

//确认接单
const returnSubmitReceive = () => {
  console.log("这里请求接单的接口");
  acceptOrder({ measureOrderId: currObj.value.measureOrderId }).then(res => {
    if (res.code === 200) {
      ElMessage({
        message: "测量单接单成功",
        type: "success"
      });
      _isOpenReceive.value = false;
      emit("refresh");
    }
  });
};

//确认删除
const returnSubmitDel = () => {
  console.log("这里请求删除接口");
  measureOrderDel({ measureOrderId: currObj.value.measureOrderId }).then(res => {
    if (res.code === 200) {
      ElMessage({
        message: "删除成功",
        type: "success"
      });
      _isOpenDel.value = false;
      emit("refresh");
    }
  });
};

//打开新的页签
const toPath = obj => {
  const routeUrl = router.resolve(obj);
  window.open(routeUrl.href, "_blank");
};
</script>

<style lang="scss" scoped>
.download-wrap {
  border: 1px solid #e1e6ed;
  border-bottom: none;
}
.products-list {
  .table-header {
    background-color: #f2f6fc;
    margin-bottom: 10px;
    border: 1px solid #e1e6ed;
    position: sticky;
    top: 0;
    z-index: 2;
  }
  .table-body {
    // max-height: calc(100vh - 345px);
    .green {
      color: #19cc15;
    }
    .orange {
      color: #ff9900;
    }
    .red {
      color: #ff3333;
    }
    // overflow: auto;
    .list-item {
      margin-bottom: 10px;
      & > div {
        border: 1px solid #e1e6ed;
        &.row {
          & > div:not(:last-of-type) {
            border-right: 1px solid #e1e6ed;
          }
        }
        & > div {
          display: flex;
          align-items: center;
        }
      }
      .col1 {
        .el-button {
          width: 100%;
        }
        & > div {
          display: flex;
        }

        .left {
          img {
            width: 80px;
            height: 80px;
            border-radius: 4px;
          }
        }
        .right {
          padding-left: 18px;
          .name {
            font-size: 14px;
            color: #3d3d3d;
            margin-bottom: 5px;
          }
          p {
            font-size: 12px;
            color: #86909c;
            margin-bottom: 5px;
          }
        }
      }
      .tpis-head {
        line-height: 24px;
        padding: 8px 20px;
        border-bottom: none;
        background-color: #f5f7fa;
      }
      .tips {
        padding: 8px 20px;
        border-top: none;
        line-height: 22px;
        box-sizing: border-box;
        color: #4e5969;
      }
    }
  }
  .table-header,
  .table-body {
    & > div > div.row {
      display: grid;
      grid-template-columns: 160px 1fr 60px 60px 1fr 1fr 1fr 60px 80px 1fr 1fr 120px;
      & > div {
        padding: 14px 8px;
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        box-sizing: border-box;
        :deep(.el-button.el-button--primary.is-link.border) {
          span {
            // white-space: break-spaces;
            // word-break: break-all;
            display: inline-block;
            max-width: 150px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            border-bottom: 1px solid #007dff;
          }
        }
      }
    }
  }
}
</style>
